﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace CTManagerSoln
{
    public class clsPhongban
    {
        
        internal static int Insert(string MaPhongban,string Tenphongban, string Ghichu)
        {            
            int stateUpdate;
            string sql = "INSERT into tblPhongban (MaPhongban,Tenphongban,Mota) Values(@MaPhongban,@Tenphongban,@Mota)";

            using (OleDbConnection conn = new OleDbConnection(Commons.DBConnect.ConnectionString))
            {
                using (OleDbCommand command = new OleDbCommand(sql, conn))
                {
                    conn.Open();
                    command.Parameters.Add("@MaPhongban", OleDbType.VarChar).Value = MaPhongban;
                command.Parameters.Add("@Tenphongban", OleDbType.VarChar).Value = Tenphongban;
                command.Parameters.Add("@Mota", OleDbType.VarChar).Value = Ghichu;
                    stateUpdate = command.ExecuteNonQuery();
                }
            }
            return stateUpdate;
        }

        internal static DataTable GetAllPhongban()
        {
            DataTable dtReturn = new DataTable();
            string selectString = "SELECT MaPhongban,Tenphongban, Mota FROM tblPhongban";
            OleDbCommand cmd = new OleDbCommand(selectString, Commons.DBConnect);
            OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
            adapter.Fill(dtReturn);
            return dtReturn;
        }

        internal static DataTable GetPhongban(string maphongban)
        {
            DataTable dt = new DataTable();
            string sql = string.Format("select MaPhongban,Tenphongban from tblPhongban where MaPhongban='{0}'", maphongban);

            OleDbConnection con = new OleDbConnection(Commons.DBConnect.ConnectionString);
            OleDbDataAdapter adap = new OleDbDataAdapter(sql, con);
            adap.Fill(dt);
            return dt;
        }

        internal static int Update(string maphongban, string tenphongban, string mota)
        {
            int stateUpdate;
            string sql = "update tblPhongban set Tenphongban=@tenphongban,Mota=@mota where MaPhongban=@maphongban";
            using (OleDbConnection conn = new OleDbConnection(Commons.DBConnect.ConnectionString))
            {
                using (OleDbCommand command = new OleDbCommand(sql, conn))
                {
                    conn.Open();
                    command.Parameters.Add("@tenphongban", OleDbType.VarChar).Value = tenphongban;
                    command.Parameters.Add("@mota", OleDbType.VarChar).Value = mota;
                    command.Parameters.Add("@maphongban", OleDbType.VarChar).Value = maphongban;
                    stateUpdate = command.ExecuteNonQuery();
                }
            }
            return stateUpdate;
        }
    }
}
